﻿using DayuCode.Designer;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DayuCode.Designer
{
    public class UserConfig
    {
        public string LoginId;
        public long AppId;

        public static UserConfig Instance
        {
            get;set;
        }

        private static string confgFilePath
        {
            get
            {
                return Path.Combine(AppInstance.StartupPath, "userconfig.json");
            }
        }
        public static UserConfig LoadConfig()
        {
            UserConfig config = null;
            if (File.Exists(confgFilePath))
            {
                string jsonStr = File.ReadAllText(confgFilePath);
                if (!string.IsNullOrEmpty(jsonStr))
                {
                    try
                    {
                        config = JsonConvert.DeserializeObject<UserConfig>(jsonStr);
                    }
                    catch
                    {

                    }
                }
            }
            if(config == null)
            {
                config = new UserConfig();
            }
            return config;
        }

        public static void SaveConfig(UserConfig config)
        {
            string jsonStr = JsonConvert.SerializeObject(config, Formatting.Indented);
            File.WriteAllText(confgFilePath, jsonStr);
        }
    }
}
